package dd.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

import xml.Message;

import dd.model.Event;
import dd.model.Model;
import dd.view.addChoiceScreen;

public class SetChoiceButtonController implements ActionListener {
	/**
	 * for a open mode,call when a player have decided his choice(including the moderator) send this choice to the
	 * server waiting for response
	 */
	addChoiceScreen choiceScreen;
	Event event;

	public SetChoiceButtonController(addChoiceScreen choiceScreen, Event event) {
		this.event = event;
		this.choiceScreen = choiceScreen;
	}

	public void actionPerformed(ActionEvent arg0) {
		// get from textfield
		JTextField choiceTF = choiceScreen.getChoiceTextField();
		String choice = (String) (choiceTF.getText());
		// send addChoiceRequest
		Model model = Model.getInstance();
		Event e = model.getEvent();
		String postion = String.valueOf(e.getPostion());
		String xmlString = Message.requestHeader() + "<addChoiceRequest id='" + e.getEventID() + "'" + " number='"
				+ postion + "' choice='" + choice + "'/>" + "</request>";
		Message m = new Message(xmlString);
		model.getServerAccess().sendRequest(m);
		// this.choiceScreen.deletebutton();
		// addEdgesScreen form = new addEdgesScreen(event);
		// form.setVisible(true);

		// close mainframe window
		// choiceScreen.dispose();
		Model.getInstance().setaddChoiceScreen(this.choiceScreen);
	}
}
